 package com.gmrz.uaf.protocol.v1.json;
 
 import java.lang.reflect.Type;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;

import com.google.gson.InstanceCreator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.inject.Inject;
import com.gmrz.uaf.crypto.CryptoEngine;
import com.gmrz.util.Convert;
 
 public class CertificateAdapter
   implements InstanceCreator<Certificate>, JsonSerializer<Certificate>, JsonDeserializer<Certificate>
 {
 
   @Inject
   private static CryptoEngine cryptoEngine = null;
 
   public Certificate createInstance(Type type)
   {
     return null;
   }
 
   public JsonElement serialize(Certificate src, Type typeofSrc, JsonSerializationContext context)
   {
     try
     {
       return context.serialize(Convert.toBase64(src.getEncoded()));
     } catch (CertificateEncodingException e) {
       e.printStackTrace();
     }throw new JsonParseException("Error encoding certificate: " + src);
   }
 
   public Certificate deserialize(JsonElement json, Type typeofT, JsonDeserializationContext context)
     throws JsonParseException
   {
     try
     {
       return cryptoEngine.getAttestationCertHandler().decode(Convert.fromBase64(json.getAsString()));
     } catch (CertificateException e) {
       e.printStackTrace();
     }throw new JsonParseException("Error parsing certificate: " + json.getAsString());
   }
 }
